package com.sodaopen.crm.customer.controller;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sodaopen.crm.common.entity.AjaxResult;
import com.sodaopen.crm.customer.entity.Customer;
import com.sodaopen.crm.customer.service.ICustomerService;
import com.sodaopen.crm.utils.Const;

import cn.hutool.core.collection.ListUtil;
import io.jsonwebtoken.Claims;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SodaJava
 * @since 2020-07-04
 */
@RestController
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @GetMapping("/customers")
    public Object queryCustomer(
            @RequestParam Map<String, Object> params,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {

        List<String> allowColumn = ListUtil.toList("star", "lastFollow");
        if(params.get("orderCloumn") != null && !allowColumn.contains(params.get("orderColumn"))){
            return AjaxResult.BAD_REQUEST("排序参数有误");
        }
        Page<Customer> result = customerService.queryCustomer(params, page, limit);

        return AjaxResult.OK("查询成功！", result);
    }


    @PostMapping("/customers")
    public Object saveCustomer(@RequestBody Customer customer, HttpServletRequest request) {
        Claims claims = (Claims)request.getAttribute(Const.LOGIN_USER);
        customer.setFollower(Integer.parseInt(claims.getId()));
        customerService.addCustomer(customer);
        return AjaxResult.OK("添加客户成功！", customer);
    }

    @GetMapping("/customers/{id}")
    public Object getCustomer(@PathVariable("id") Integer id){
        Customer customer = customerService.getById(id);
        if(customer == null){
            return AjaxResult.NOT_FOUND("客户不存在！");
        }
        return AjaxResult.OK("查询成功！", customer);
    }

    @PutMapping("/customers/{id}")
    public Object updateUser(@PathVariable("id") Integer id,
                             @RequestBody Customer customer){

        Customer dbCustomer = customerService.getById(id);
        if(dbCustomer == null){
            return AjaxResult.NOT_FOUND("客户不存在！");
        }

        //复制要被更新的字段
        dbCustomer.setName(customer.getName());
        dbCustomer.setMobile(customer.getMobile());
        dbCustomer.setEmail(customer.getEmail());
        dbCustomer.setCompany(customer.getCompany());
        dbCustomer.setCountry(customer.getCountry());
        dbCustomer.setSkype(customer.getSkype());
        dbCustomer.setWhatsapp(customer.getWhatsapp());
        dbCustomer.setStar(customer.getStar());

        //空的字段不会更新到数据库
        boolean flag = customerService.updateById(dbCustomer);
        if(flag){
            return AjaxResult.OK("更新客户信息成功！", dbCustomer);
        }else{
            return AjaxResult.ERROR("更新客户信息失败！");
        }

    }

    @DeleteMapping("/customers/{id}")
    public Object deleteCustomer(@PathVariable("id") Integer id){
        boolean flag = customerService.removeById(id);
        if(flag){
            return AjaxResult.OK("删除成功！");
        }else{
            return AjaxResult.ERROR("删除失败！");
        }
    }
    
    @RequestMapping("/customers/country")
    public Object country(){
    	List<String> list = customerService.distinctCountry();
    	return AjaxResult.OK("查询成功", list);
    }
}
